package com.gavinwind.inspay.common.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * 【对象工具类，提供对象转换map，map转换对象功能】
 * @author chihouliang
 * @date 2016年7月4日 下午3:03:26 
 * @version 1.0
 */
public class ObjectUtil {

    private ObjectUtil() {}

    /**
     * 对象序列化，把对象转为byte数组
     * @param cObject 待序列化的对象
     * @return 对象序列化byte数组
     */
    public static byte[] serialize(Object cObject) {

        ObjectOutputStream tObjectOutputStream = null;
        ByteArrayOutputStream tByteArrayOutputStream = null;
        try {

            // 序列化
            tByteArrayOutputStream = new ByteArrayOutputStream();
            tObjectOutputStream = new ObjectOutputStream(tByteArrayOutputStream);
            tObjectOutputStream.writeObject(cObject);
            byte[] tBytes = tByteArrayOutputStream.toByteArray();

            return tBytes;
        } catch (Exception e) {

            throw new RuntimeException(e.getMessage(), e);
        }finally{
            FileUtil.closeOutputStream(tObjectOutputStream);
        }
    }

    /**
     * 对象byte数组反序列化为对象
     * @param cBytes 对象byte数组
     * @return 反序列化对象
     */
    public static Object unserialize(byte[] cBytes) {
        ByteArrayInputStream tByteArrayInputStream = null;
        ObjectInputStream tObjectInputStream = null;
        try {

            // 反序列化
            tByteArrayInputStream = new ByteArrayInputStream(cBytes);
            tObjectInputStream = new ObjectInputStream(tByteArrayInputStream);

            return tObjectInputStream.readObject();
        } catch (Exception e) {

            throw new RuntimeException(e.getMessage(), e);
        }finally{
            FileUtil.closeInputStream(tObjectInputStream);
        }

    }

    /**
     * 
     * 对象转换成map对象
     * @param t
     * @return
     * @throws IllegalAccessException 
     * @throws IllegalArgumentException 
     */
    public static <T> Map<String, Object> objectToMap(T cObject) {
        if (cObject == null) {
            return null;
        }
        Map<String, Object> tMap = new HashMap<String, Object>();
        Class<?> tClass = cObject.getClass();
        Field[] tFields = tClass.getDeclaredFields();
        for (int i = 0; i < tFields.length; i++) {
            Field tField = tFields[i];
            tField.setAccessible(true);
            String tName = tField.getName();
            Object tObj = null;

            try {
                tObj = tField.get(cObject);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            tMap.put(tName, tObj);
        }

        return tMap;
    }

    /**
     * 
     * 方法功能描述
     * @param cMap
     * @param cBeanClass
     * @return
     * @throws Exception
     */
    public static Object mapToObject(Map<String, Object> cMap, Class<?> cBeanClass) throws Exception {
        if (cMap == null)
            return null;

        Object tObj = cBeanClass.newInstance();

        Field[] tFields = tObj.getClass().getDeclaredFields();
        for (Field tField : tFields) {
            int tMod = tField.getModifiers();
            if (Modifier.isStatic(tMod) || Modifier.isFinal(tMod)) {
                continue;
            }
            tField.setAccessible(true);
            tField.set(tObj, cMap.get(tField.getName()));
        }

        return tObj;
    }

}
